package com.webdemo9.contrller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.webdemo9.bean.UsersBean;
import com.webdemo9.dao.UsersDao;
import com.webdemo9.dao.impl.UsersDaoFactory;
/**
 * 注册用户
 * @author lovemanai
 *
 */
public class Register extends HttpServlet {

	private UsersDao usersDao = UsersDaoFactory.getUsersDao();
	
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String characterEncoding = this.getServletContext().getInitParameter("characterEncoding").toString();
		request.setCharacterEncoding(characterEncoding);
		String username,pwd,gender,birth,phone,addr;
		username = request.getParameter("username");
		if(usersDao.checkUsername(username)) {
			response.sendRedirect("reg.jsp?errorcode=3");
			return;
		}
		pwd = request.getParameter("pwd");
		gender = request.getParameter("gender");
		birth = request.getParameter("birth");
		phone = request.getParameter("phone");
		addr = request.getParameter("addr");
		UsersBean ub = new UsersBean();
		ub.setUsername(username);
		ub.setPwd(pwd);
		ub.setGender(gender);
		ub.setBirth(birth);
		ub.setPhone(phone);
		ub.setAddr(addr);
		if(usersDao.reg(ub) == 1) {
			request.getSession().setAttribute("username", username);
			response.sendRedirect("listUsers");
		}
		
	}

}
